package manager;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import constants.Globals;

import util.CBCrypto;
import util.HibernateUtil;
import domain.UserDAO;
import exception.IBToolsException;

@SuppressWarnings("unchecked")
public class UserManager {

	public static UserDAO getUserInfoFromDB(String userCode) throws IBToolsException{
		UserDAO user = null;
    	Session session = HibernateUtil.getDEVSession();
    	Transaction tx = null;
    	try {
			tx = HibernateUtil.beginDEVTransaction(session);
			List<UserDAO> userObjList = (List<UserDAO>) session.createCriteria(UserDAO.class)
	    			.add(Restrictions.eq("userCode", userCode)).list();
			
	    	if(userObjList.size() > 0){
	    		user = (UserDAO) userObjList.get(0);
		    	if(userObjList.size() > 1){
		    		session.delete(userObjList.get(1));
		    	}
	    	}
			tx.commit();
			
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		}
    	return user;
	}

	public static String getEncryptedFaudPassFromDB(String userCode) throws IBToolsException{
		String password = null;
		UserDAO user = getUserInfoFromDB(userCode);
    	if(user != null){
    		password = user.getPass();
    	}
    	return password;
	}

	public static String defineEncryptedFaudPass(String userCode) throws IBToolsException{
		UserDAO user = getUserInfoFromDB(userCode);
		String password = null;
    	if(user == null || user.getPass() == null){
    		password = CBCrypto._p(SessionManager.getEncryptedFaudPass());
    		saveUserToDB(new UserDAO(null, userCode, password, Boolean.TRUE));
    	}
    	else {
    		password = user.getPass();
    	}
		return password;
	}

	public static void updatePasswordToDB(String userCode, String password, boolean useDefaultUser) throws IBToolsException{
		UserDAO user = getUserInfoFromDB(userCode);
		if(password != null){
			if(password.equals(Globals.EMPTY_STRING))
				password = null;
			else
				password = CBCrypto._p(password);
		}
		if(user == null)
			user = new UserDAO(null, userCode, password, useDefaultUser);
		else {
			user.setPass(password);
			user.setUseDefaultUser(useDefaultUser);
		}
		saveUserToDB(user);
	}

	public static void savePasswordToDB(String userCode, String password, boolean useDefaultUser) throws IBToolsException{
		if(password != null){
			if(password.equals(Globals.EMPTY_STRING))
				password = null;
			else
				password = CBCrypto._p(password);
		}
		saveUserToDB(new UserDAO(null, userCode, password, useDefaultUser));
	}

	public static void saveUserToDB(UserDAO user) throws IBToolsException{

    	Session session = HibernateUtil.getDEVSession();
        Transaction tx = null;
        
        try {
			tx = HibernateUtil.beginDEVTransaction(session);
			session.saveOrUpdate(user);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			throw e;
		}
	}
	
	public static UserDAO newUserInstance(String userCode) throws IBToolsException{
		UserDAO user = new UserDAO(userCode, null, true);
		return user;
	}
}
